package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 1.判断当前加入到购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 1.1获取当前用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 1.2调用Mapper 存入返回的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        // 2.如果已经存在 增加数量number即可
        if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
            return;
        }

        // 3.判断当前添加到购物车的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        if (dishId != null) {
            // 3.1是菜品，查询菜品信息
            Dish dish = dishMapper.getById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        } else {
            // 3.2是套餐，查询套餐信息
            SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
            shoppingCart.setAmount(setmealVO.getPrice());
        }
        // 4.如果不存在，则添加到购物车，数量默认为1
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        // 获取用户当前id来查询购物车列表
        Long userId = BaseContext.getCurrentId();
        if (userId != null) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            return shoppingCartMapper.list(shoppingCart);
        }
        throw new RuntimeException("用户未登录");
    }

    /**
     * 根据用户id来清空购物车
     */
    @Override
    public void delete() {
        // 获取用户id 根据用户id清理购物车
        Long userId = BaseContext.getCurrentId();
        if (userId != null) {
            shoppingCartMapper.deleteByUserId(userId);
        }
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 1.获取用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 2.根据用户id来进行删除操作
        if (userId != null) {
            // 2.1 获取该商品的数据
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart cart = list.get(0);
            if (cart.getNumber() == 1) {
                // 2.2 如果购物车中数量为1，则删除该菜品
                shoppingCartMapper.deleteByUserIdAndDishId(shoppingCart);
            } else {
                // 2.3 如果购物车中数量大于1，则数量减1
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            }
        }
    }
}
